var time = new Date().valueOf();
threads.start(function () {
    //在新线程执行的代码
    while (true) {
        click("下一步")
        click("知道了")
        click("同意")
        click("始终允许")
        if (text("安装成功，点击打开").findOne(100)) {
            launchApp("收音机")
        }
        if (id("detail_download_button").findOne(100)) {
            log("找到下载按钮")
            var 安装 = id("detail_download_button").findOne(2000)
            if (安装) {
                安装.click()
                sleep(2000)
                back()
                launchApp("收音机")
            }
        }
        if (id("app_close").findOne(100)) {
            var app_close = id("app_close").findOne(1000);
            app_close.click()
        }
        if (!device.isScreenOn()) {
            log("息屏了？？？？？？");
            device.wakeUp();
            swipe(500, 500, 500, 1000, 1000)
            sleep(1000)
        }
        sleep(2000)
    }

}
)

sleep(1000)


var frequency = 0
收音机自动下载();


function 收音机自动下载() {

    操作app("停止", "收音机")
    操作app("清除", "收音机")
    重置广告标识符();
    下载app();


}


function 重置广告标识符() {
    sleep(1000);
    app.startActivity({
        action: "android.intent.action.VIEW",
        data: "hwpps://oaid_setting",
        packageName: "com.huawei.hwid"
    });
    sleep(1000)
    click("重置广告标识符");
    sleep(1000);
    click("重置");
    sleep(3000);
}
function 下载app() {

    操作app("启动", "收音机")
    while (!textContains("剩余矿锤").findOne(10000)) {

        sleep(10000);
        app.startActivity({
            action: "android.intent.action.VIEW",
            data: "hwfmradio://com.huawei.android.FMRadio/showH5?pver=121000000&portal=hwmusic&from=com.huawei.musicplatform.campaignh5&needback=0&channelid=1&url=https://campaign-music.hicloud.com/camp/360720002640343040/index.html?appsafearguments=true&activity=true&operator=f2&share=false&transparentstatusbar=true&hideactionbar=true&needback=true&isext=false",
            packageName: "com.huawei.android.FMRadio"
        });
        console.log("已打开收音机");
        sleep(3000)
    }
    console.log("等待下载app");
    sleep(10000)
    var 抽奖次数 = textContains("剩余矿锤").findOne(60000);
    //获取到抽奖信息
    if (抽奖次数) {
        抽奖次数 = 抽奖次数.text().split("锤")[1].split("个")[0]
        抽奖次数 = 抽奖次数 * 1
        log("已有抽奖数为-" + 抽奖次数);
        //抽奖次数小于10 继续下载
        if (抽奖次数 < 6) {

            log("等待下载app")
            sleep(5000)
            var 下载体验 = textContains("下载体验").find();//获取下载体验集合
            var count = 下载体验.length;
            log("下载体验个数为--" + count);
            if (count > 0) {
                var surplusCount;
                //如果(总数-已下载 )>=可下载 -》 全下载。。(总数-已下载 )<可下载 -》 下载(总数-已下载 )
                if (抽奖次数 == 5) {
                    if (textContains("下载体验").find().length > 0) {
                        
                            surplusCount = count;
                            textContains("下载体验").find().click();
                        
                    }
                } else {
                    surplusCount = 5 - 抽奖次数;
                    log("剩余抽奖次数为--" + surplusCount);
                    if (textContains("下载体验").find().length > 0) {
                        if (surplusCount >= count) {
                            surplusCount = count;
                            textContains("下载体验").find().click();
                        } else {
                            for (let i = 0; i < surplusCount; i++) {
                                log("点击下载体验--", i)
                                textContains("下载体验").findOne(1000).click();
                                sleep(1000)
                            }
                        }
                    }
                }

                log("等待app下载完成");

                var 打开 = textContains("打开").find();
                var countAll = 打开.length;
                log("需要打开的总数量", countAll);
                //判断下载完成数量f
                var time1 = new Date().valueOf()

                while (countAll <= surplusCount) {
                    var time2 = new Date().valueOf()
                    log("等待所有app下载完成");
                    sleep(10000); 
                    if (time2 - time1 > 300000 ) {
                        break;
                    }
                    if (text("好的").findOne(100)) {
                        log(textContains("好的").findOne(1000))
                        收音机自动抽奖()
                        threads.shutDownAll()
                        return

                    }
                    if (textContains("达到上限").findOne(100)) {
                        log(textContains("达到上限").findOne(1000))
                        收音机自动抽奖()
                        threads.shutDownAll()
                        return

                    }
                    //是否存在下载失败
                    var 下载失败 = textContains("下载失败").find();
                    打开 = textContains("打开").find();

                    countAll = 打开.length;
                    log("已安装数量" + countAll);
                    if (下载失败.empty) {
                        var num = countAll + 下载失败.length;
                        log("已下载的总数num--", num)
                        log("需要的总数surplusCount--", surplusCount)
                        if (num == surplusCount) {
                            break;
                        }
                    }
                }

                log("******************************")
                if (textContains("打开").find().length > 0) {
                    textContains("打开").find().click();
                }
                sleep(2000);
                home();
                sleep(10000);
                卸载app();

                if (抽奖次数 * 1 + countAll * 1 < 10) {
                    //抽奖次数不满10次，继续下载
                    收音机自动下载()
                } else {
                    //抽奖次数已满10次，直接抽奖
                    收音机自动抽奖()
                    threads.shutDownAll()
                }

            } else {//下载列表为空
                if (frequency == 4) {
                    log("第三次没有下载APP")
                    收音机自动抽奖()
                    threads.shutDownAll()
                    return
                }
                frequency += 1
                log("下载列表为空,第几次", frequency)
                收音机自动下载()
            }
        } else {//抽奖次数已满
            log("抽奖次数已满")
            收音机自动抽奖()
            threads.shutDownAll()
            return
        }

    } else {//为获取到抽奖次数，可能页面加载失败，重新进入下载流程
        收音机自动下载()
    }

}


threads.shutDownAll()


function 操作app(操作, app名称) {
    var 包名 = getPackageName(app名称);
    log(包名)
    switch (操作) {
        case "启动":
            shell('am start ' + 包名, true);
            sleep(10000)
            break;
        case "清除":
            操作app("停止", "设置")
            log("停止设置完成")
            openAppSetting(包名);
            点击文字("存储");
            点击文字("删除数据");
            点击文字("确定");
            sleep(10000)
            break;
        case "卸载":
            shell('pm uninstall ' + 包名, true);
            sleep(10000)
            break;
        case "停止":
            shell('am force-stop ' + 包名, true);
            sleep(10000)
            break;
        default:
            log("默认")
    }
}



function 点击文字(文字) {
    sleep(1000);
    var 点击的文字 = textContains(文字).findOne(5000);
    if (点击的文字) {
        var x = 点击的文字.bounds().centerX();
        var y = 点击的文字.bounds().centerY();
        sleep(1000);
        click(x, y);
        console.log("已点击" + 文字);
    } else {
        console.log("未发现" + 文字);
    }

}

function 卸载app() {
    //sleep(30000);
    log("开始卸载");
    var pm = context.getPackageManager()
    var appList = pm.getInstalledApplications(0)

    for (let i = 0; i < appList.size(); i++) {
        var app_ = appList.get(i)
        var appInfo = {
            appName: app_.loadLabel(pm),
            packageName: app_.packageName,
            isSystemApp: app_.isSystemApp(),
            lastUpdateTime: pm.getPackageInfo(app_.packageName, 0).lastUpdateTime
        }
        if (appInfo.lastUpdateTime > time) {
            console.log(appInfo.appName + "卸载中");

            操作app("卸载", appInfo.appName)
        }
    }



    sleep(15000);
    log("卸载完成");

}
function 收音机自动抽奖() {
    log("开始抽奖")
    操作app("清除", "收音机")
     重置广告标识符();
    操作app("启动", "收音机")
    sleep(10000)


    while (!textContains("剩余矿锤").exists()) {
        app.startActivity({
            action: "android.intent.action.VIEW",
            data: "hwfmradio://com.huawei.android.FMRadio/showH5?pver=121000000&portal=hwmusic&from=com.huawei.musicplatform.campaignh5&needback=0&channelid=1&url=https://campaign-music.hicloud.com/camp/360720002640343040/index.html?appsafearguments=true&activity=true&operator=f2&share=false&transparentstatusbar=true&hideactionbar=true&needback=true&isext=false",
            packageName: "com.huawei.android.FMRadio"
        });
        sleep(20000)
    }
    while (textContains("剩余矿锤").findOne(100)) {
        log("抽奖中")
        if (textContains("剩余矿锤0个").findOne(100)) {
            log("抽奖完成")
            sleep(3000);
            break;
        }
        click(random(149, 942), random(1122, 1668))
        //click(random(324, 753), random(573, 963))
        log("点击1111111111111")
        sleep(1000);
        click(843, 730);
    }


}







操作app("停止", "收音机")